package com.cmc.cloud.cmclink.doc.util;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.Map;

import com.cmc.cloud.cmclink.doc.constants.CommonConstants;

/**
 * Date工具类
 * 
 * @author dev89
 * @date 2025/10/30 18:46
 **/
public class DateUtils {
    private static final String DATE_TIME_FORMATTER = "yyyy-MM-dd";

    /**
     * 获取当前时间前后指定月份的日期范围
     * 
     * @param monthsBefore 前推月数
     * @param monthsAfter 后推月数
     * @return 包含start和end的Map
     */
    public static Map<String, String> getDateRange(int monthsBefore, int monthsAfter) {
        LocalDate today = LocalDate.now();
        LocalDate start = today.minusMonths(monthsBefore);
        LocalDate end = today.plusMonths(monthsAfter);

        DateTimeFormatter formatter = DateTimeFormatter.ofPattern(DATE_TIME_FORMATTER);

        Map<String, String> result = new HashMap<>(16);
        result.put(CommonConstants.DATE_TIME_START, start.format(formatter));
        result.put(CommonConstants.DATE_TIME_END, end.format(formatter));

        return result;
    }

    /**
     * 获取当前时间
     *
     * @param pattern pattern
     * @return java.lang.String
     */
    public static String getCurrentTimeStr(String pattern) {
        LocalDateTime now = LocalDateTime.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern(pattern);
        return now.format(formatter);
    }
}
